package com.umeng.comm.ui.fragments;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qianxx.draw.R;
import com.qianxx.base.BaseRefreshFrg;
import com.qianxx.base.IConstants;
import com.qianxx.base.Urls;
import com.qianxx.base.request.Config;
import com.qianxx.base.request.RM;
import com.qianxx.base.request.RequestBean;
import com.qianxx.base.utils.SPUtil;
import com.umeng.comm.core.utils.CommonUtils;
import com.umeng.community.example.adapter.NewInfoAdapter;
import com.umeng.community.example.bean.NewInfo;
import com.umeng.community.example.bean.NewInfoBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zhangbingyong on 16/5/4.
 */
public class InformationFragment extends BaseRefreshFrg{

    private int nowPage = 1;
    private NewInfoAdapter userInfoAdapter;
    private List<NewInfo> newInfos = new ArrayList<NewInfo>();
    //未登录时显示
    private TextView mTvEmpty;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.frag_info, null);
            initRefreshLayout(mView,getActivity());
            initView();
            loadData();
            if (TextUtils.isEmpty(SPUtil.getInstance().getToken()) || !CommonUtils.isLogin(getActivity())) {
                mTvEmpty.setVisibility(View.VISIBLE);
                mRefreshLayout.setVisibility(View.GONE);
            }else {
                mTvEmpty.setVisibility(View.GONE);
                mRefreshLayout.setVisibility(View.VISIBLE);
            }
        }
        return mView;
    }

    private void initView() {
        userInfoAdapter = new NewInfoAdapter(getActivity());
        mRefreshListView.setAdapter(userInfoAdapter);
        mTvEmpty = (TextView)mView.findViewById(R.id.empty_tv);
    }

    private void loadData() {
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("nowPage",nowPage+"");
        requestDataWithoutLoading(IConstants.REQUEST, Urls.NEWSINFO_LIST, RM.GET, NewInfoBean.class, map);
    }

    @Override
    public void requestSuccess(RequestBean baseBean, Config config) {
        onRefreshComplete();
        onLoadComplete();
        NewInfoBean newInfoBean = (NewInfoBean)baseBean;
        newInfos.addAll(newInfoBean.getData());
        userInfoAdapter.setData(newInfos);
    }

    @Override
    public void onRefresh() {
        newInfos.clear();
        nowPage = 1;
        loadData();
    }

    @Override
    public void onLoad() {
        nowPage ++;
        loadData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(SPUtil.getInstance().getToken()) || !CommonUtils.isLogin(getActivity())) {
            mTvEmpty.setVisibility(View.VISIBLE);
            mRefreshLayout.setVisibility(View.GONE);
        }else {
            mTvEmpty.setVisibility(View.GONE);
            mRefreshLayout.setVisibility(View.VISIBLE);
        }
    }
}
